package com.hns.phone.pushMsg.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hns.phone.pushMsg.vo.PushMessageParamsVo;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

@Component
public class IOSGroupNotification extends IOSNotification {
	
	@Autowired
	private BasePushClient client;
	
//	public IOSGroupNotification(String appkey,String appMasterSecret) throws Exception {
//			setAppMasterSecret(appMasterSecret);
//			setPredefinedKeyValue("appkey", appkey);
//			this.setPredefinedKeyValue("type", "groupcast");	
//	}
	
//	public void setFilter(JSONObject filter) throws Exception {
//    	setPredefinedKeyValue("filter", filter);
//    }
	
	/**
	 * 发送信息动作
	 * @param params
	 * @throws Exception
	 * @see com.hns.can.pushMsg.util.AndroidNotification#sendMsg(com.hns.can.pushMsg.vo.PushMessageParamsVo)
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@Override
	public String sendMsg(PushMessageParamsVo params) throws Exception {
//		this.setPredefinedKeyValue("type", "listcast");
		//组装发送设备tokens，多个device_token用英文逗号分隔
//		this.setPredefinedKeyValue("device_tokens", params.getTokenStr());
		setAppMasterSecret(params.getAppMasterSecret());
		setPredefinedKeyValue("appkey", params.getAppkey());
		this.setPredefinedKeyValue("type", "groupcast");
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		
//		if("MDG00004".equals(params.getGroupType())){
//			for(String tag : params.getSelectedTags()){
//				JSONObject testTag = new JSONObject();
//				testTag.put("tag", tag);
//				tagArray.put(testTag);
//			}
//		}else{
//			JSONObject testTag = new JSONObject();
//			testTag.put("tag", params.getGroupType());
//			tagArray.put(testTag);
//		}
		JSONObject testTag = new JSONObject();
		testTag.put("tag", params.getGroupType());
		tagArray.put(testTag);
		
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
		setPredefinedKeyValue("filter", filterJson);
		this.setAlert(params.getContent());
		this.setCustomizedField("title", params.getTitle());
//		this.setCustomizedField("msg_id", params.getMsgId());
//		if(PubUtil.isNotEmpty(params.getWebUrl())){
//			this.setCustomizedField("url", params.getWebUrl());
//		}else{
//			this.removeCustomizedField("url");
//		}
		
//		this.setCustomizedField("type", "message");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.setCustomizedField("creat_time", format.format(new Date()));
		
		this.setBadge(0);
		this.setSound("default");
		this.setProductionMode();
		
		if(PubUtil.isNotEmpty(params.getDelayTime())){
			this.setStartTime(params.getDelayTime() + "00:00:00");
			String expireTime = DateTool.calculateByDate(params.getDelayTime(), 3);
			this.setExpireTime(expireTime + "23:59:59");
		}
		String taskId = client.sendMsg(this);
		return taskId;
	}
	
}
